package top.lbqaq.service;

import top.lbqaq.dao.BaseDao;
import top.lbqaq.dao.ProductDao;
import top.lbqaq.pojo.Product;
import top.lbqaq.util.pageBean;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Gor
 */
public class ProductService {
    private final ProductDao productDao = new ProductDao();


    public List<Product> selectByName(String name) {
        Connection connection = BaseDao.getConnection();
        try {
            List<Product> allProducts = productDao.getAllByName(connection,name);
            if (allProducts.isEmpty()) {
                return null;
            } else {
                return allProducts;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public Product selectById(int id) {
        Connection connection = BaseDao.getConnection();
        try {
            Product product = productDao.getAllById(connection,id);
            if (product==null) {
                return null;
            } else {
                return product;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public int delectByNo(String No) {
        Connection connection = BaseDao.getConnection();
        int n=0;
        try {
            n = productDao.delectAllByNo(connection,No);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return n;
    }
    public int updateById(Product product) {
        Connection connection = BaseDao.getConnection();
        int n=0;
        try {

           // List<Product> allProducts = productDao.updateAllById(connection,product);
             n=productDao.updateAllById(connection,product);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return n;
    }
    public int insertproduct(Product product) {
        Connection connection = BaseDao.getConnection();
        int n=0;
        try {

            // List<Product> allProducts = productDao.updateAllById(connection,product);
            n=productDao.insert(connection,product);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return n;
    }

    //    public List<Product> selectAllProduct() {
//        Connection connection = BaseDao.getConnection();
//        try {
//            List<Product> allProducts = productDao.getAllProduct(connection);
//            if (allProducts.isEmpty()) {
//                return null;
//            } else {
//                return allProducts;
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
    public pageBean<Product> selectAllProduct(int pageNum,int pagesize){
        Connection connection = BaseDao.getConnection();
        try {
            List<Product> allProducts = productDao.getAllProduct(connection);
            int totalRecord =allProducts.size();
            pageBean pb=new pageBean(pageNum,pagesize,totalRecord);
            int startIndex=pb.getStartIndex();
            pb.setList(productDao.getAllProductpg(startIndex,pagesize,connection));
            return pb;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }
    public pageBean<Product> selectByCategory1(String productCategory1,int pageNum,int pagesize) {
        Connection connection = BaseDao.getConnection();
        try {
            List<Product> allProducts = productDao.getAllCategory1(connection,productCategory1);
            int totalRecord =allProducts.size();
            pageBean pb=new pageBean(pageNum,pagesize,totalRecord);
            int startIndex=pb.getStartIndex();
            pb.setList(productDao.getAllCategory1pg(connection,productCategory1,startIndex,pagesize));
            if (allProducts.isEmpty()) {
                return null;
            } else {
                return pb;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public List<Product> selectByCategory2(String productCategory2) {
        Connection connection = BaseDao.getConnection();
        try {
            List<Product> allProducts = productDao.getAllCategory2(connection,productCategory2);
            if (allProducts.isEmpty()) {
                return null;
            } else {
                return allProducts;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
